package com.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.domain.Addres;
import com.domain.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.AddresService;
import com.service.Impl.AddresServiceImpl;
import com.service.Impl.UsersServiceImpl;
import com.service.UsersService;

@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		UsersService usersService = new UsersServiceImpl();
        //根据名字查询用户
        User user = usersService.findByName(username);
        int uid = user.getId();//得到用户ID值

        //查询地址信息
        AddresService adresService = new AddresServiceImpl();
        List<Addres> list = adresService.findById(uid);
        System.out.println("order地址信息；"+list);

        //创建ObjectMapper对象
        ObjectMapper mapper = new ObjectMapper();
        String list_json = mapper.writeValueAsString(list);
        response.getWriter().write(list_json);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
